home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / PICKLIST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.5 KB  |  249 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.8  $
  6. //
  7. // Implementation of classes TPickListPopup & TPickListDialog.
  8. //
  9. // Both of these modal windows allow the selction of a string from a list.
  10. // TPickListPopup uses a lean popup menu for a quick selection, while
  11. // TPickListDialog uses a listbox in a dialog with OK and Cancel buttons.
  12. //----------------------------------------------------------------------------
  13. #include <owl/pch.h>
  14. #if !defined(OWL_PICKLIST_H)
  15. # include <owl/picklist.h>
  16. #endif
  17. #if !defined(OWL_PICKLIST_RH)
  18. # include <owl/picklist.rh>
  19. #endif
  20.  
  21. OWL_DIAGINFO;
  22.  
  23. //
  24. // Construct a popup menu.
  25. // Add the title at the top of the menu.
  26. //
  27. TPickListPopup::TPickListPopup(TWindow* parent, const char far* title)
  28. :
  29.   TWindow(parent, 0, 0),
  30.   Count(0)
  31. {
  32.   PRECONDITION(Popup.GetHandle());
  33.  
  34.   if (title) {
  35.     Popup.AppendMenu(MF_GRAYED, 0, title);
  36.     Popup.AppendMenu(MF_SEPARATOR);
  37.   }
  38. }
  39.  
  40. //
  41. // Construct a popu menu.
  42. // Add the title from resource at the top of the menu.
  43. //
  44. TPickListPopup::TPickListPopup(TWindow* parent, uint titleId)
  45. :
  46.   TWindow(parent, 0, 0),
  47.   Count(0)
  48. {
  49.   PRECONDITION(Popup.GetHandle());
  50.  
  51.   char buf[_MAX_PATH];
  52.   if (!titleId || !GetApplication()->LoadString(titleId, buf, sizeof buf))
  53.     return;
  54.  
  55.   Popup.AppendMenu(MF_GRAYED, 0, buf);
  56.   Popup.AppendMenu(MF_SEPARATOR);
  57. }
  58.  
  59. //
  60. // Clear all strings from the popup
  61. //
  62. void
  63. TPickListPopup::ClearStrings()
  64. {
  65.   while (Count)
  66.     Popup.RemoveMenu(--Count, MF_BYCOMMAND);
  67. }
  68.  
  69. //
  70. // Add a string to the popup
  71. //
  72. int
  73. TPickListPopup::AddString(const char far* str)
  74. {
  75.   PRECONDITION(Popup.GetHandle());
  76.   Popup.AppendMenu(MF_ENABLED, Count++, str);
  77.   return Count;
  78. }
  79.  
  80. //
  81. // Displays the popup menu and returns the command id of the menu item the
  82. // user selected.
  83. //
  84. int
  85. TPickListPopup::Execute()
  86. {
  87.   // Create the hidden window
  88.   //
  89.   Create();
  90.  
  91.   // Reset the index result
  92.   //
  93.   Result = -2;
  94.  
  95.   // Grab current mouse location
  96.   //
  97.   TPoint cursorLoc;
  98.   GetCursorPos(cursorLoc);
  99.  
  100.   // Display section & cleanup
  101.   //
  102.   Popup.TrackPopupMenu(0, cursorLoc, 0, GetHandle());
  103. //  while (Popup.GetMenuItemCount())   
  104. //    Popup.DeleteMenu(0, MF_BYPOSITION);
  105.  
  106.   // Dispatch any pending WM_COMMAND stuck in the message queue.
  107.   //
  108.   CHECK(GetApplication());
  109.   GetApplication()->PumpWaitingMessages();
  110.  
  111.   return Result;
  112. }
  113.  
  114. //
  115. // Generic handler for WM_COMMAND messages.
  116. // Result is set to the id of the menu item clicked.
  117. //
  118. TResult
  119. TPickListPopup::EvCommand(uint id, THandle /*hWndCtl*/, uint /*notifyCode*/)
  120. {
  121. //  WARNX(OwlDocView, id > Count, 0, "TPickListPopup index invalid");
  122.   Result = id;
  123.   return 0;
  124. }
  125.  
  126. //----------------------------------------------------------------------------
  127.  
  128. //
  129. //
  130. //
  131. DEFINE_RESPONSE_TABLE1(TPickListDialog, TDialog)
  132.   EV_COMMAND(IDOK,     CmOK),
  133.   EV_COMMAND(IDCANCEL, CmCancel),
  134.   EV_LBN_DBLCLK(IDC_LIST, CmOK),
  135. END_RESPONSE_TABLE;
  136.  
  137. //
  138. // Initialize the dialog.
  139. // Sets the initial selection of the listbox.
  140. // Allocate strings if necessary.
  141. //
  142. TPickListDialog::TPickListDialog(TWindow*      parent,
  143.                                  TStringArray* strings,
  144.                                  int           initialSelection,
  145.                                  TResId        templateId,
  146.                                  const char far* title,
  147.                                  TModule*      module)
  148. :
  149.   TDialog(parent, templateId ? templateId : TResId(IDD_PICKLISTDIALOG), module),
  150.   Strings(strings ? strings : new TStringArray(10,0,10)),
  151.   NewedStrings(!strings),
  152.   List(this, IDC_LIST, module),
  153.   Result(initialSelection)
  154. {
  155.   if (title)
  156.     SetCaption(title);
  157. }
  158.  
  159. //
  160. // Deletes any allocated string.
  161. //
  162. TPickListDialog::~TPickListDialog()
  163. {
  164.   if (NewedStrings)
  165.     delete Strings;
  166. }
  167.  
  168. //
  169. // Clear all strings from the list
  170. //
  171. void
  172. TPickListDialog::ClearStrings()
  173. {
  174.   Strings->Flush();
  175.   if (List.GetHandle())
  176.     List.ClearList();
  177. }
  178.  
  179. //
  180. // Add a string to the Strings list, and to the List box if it has already been
  181. // created.
  182. //
  183. int
  184. TPickListDialog::AddString(const char far* str)
  185. {
  186.   int pos = Strings->Add(str);
  187.   if (List.GetHandle())
  188.     List.AddString(str);
  189.   return pos;
  190. }
  191.  
  192. //
  193. // Add a string to a listbox, passed via the args from ForEach.
  194. //
  195. static void
  196. AddToList(string& string, void* param)
  197. {
  198.   TListBox* list = REINTERPRET_CAST(TListBox*,param);
  199.   if (list && list->GetHandle()) // just in case
  200.     list->AddString(string.c_str());
  201. }
  202.  
  203. //
  204. // Override from TDialog.
  205. // Adds each string into the listbox.
  206. //
  207. void
  208. TPickListDialog::SetupWindow()
  209. {
  210.   TDialog::SetupWindow();
  211.  
  212.   // Add each string in Strings to the List box
  213.   //
  214.   Strings->ForEach(::AddToList, &List);
  215.  
  216.   // Set inital state of listbox
  217.   //
  218.   if (List.GetCount() > Result) {
  219.     List.SetSelIndex(Result);
  220.     List.SetCaretIndex(Result, false);
  221.   }
  222. }
  223.  
  224. //
  225. // User selected OK. Get selection from the listbox & return it.
  226. //
  227. void
  228. TPickListDialog::CmOK()
  229. {
  230.   int index = List.GetSelIndex();
  231.   if (index >= 0) {
  232.     // Save the selection index.
  233.     // Add LowerBound() because we cannot assume the lower bound is 0.
  234.     //
  235.     Result = index + Strings->LowerBound();
  236.   }
  237.   CloseWindow(Result);
  238. }
  239.  
  240. //
  241. // User selected Cancel. Return < 0 (Can't use -1 since it signals a dialog
  242. // failure)
  243. //
  244. void
  245. TPickListDialog::CmCancel()
  246. {
  247.   CloseWindow(-2);
  248. }
  249.